package cron

import (
	"3-19yk/common/models/mysql"
	"github.com/robfig/cron/v3"
	"log"
	"time"
)

func UpdateStatusCron() {
	c := cron.New()
	_, err := c.AddFunc("@every 1m", func() {
		DbUpdateStatusCron()
	})
	if err != nil {
		return
	}
	c.Start()
}
func DbUpdateStatusCron() {
	now := time.Now()
	var order []mysql.Order
	err := mysql.DB.Debug().Where("order_status", "1").Find(&order).Error
	if err != nil {
		log.Println("查询失败")
		return
	}
	for _, m := range order {
		at := m.CreatedAt
		sub := now.Sub(at)

		if sub > 30*time.Minute {
			m.OrderStatus = 2
			err := mysql.DB.Save(&m).Error
			if err != nil {
				log.Println("状态修改失败")
				return
			} else {
				log.Printf("%s状态修改成功", m.OrderSn)
				return
			}
		}
	}
}
